//
//  FFNetworking.m
//  KeChuangManage
//
//  Created by shang on 2016/11/26.
//  Copyright © 2016年 shang. All rights reserved.
//

#import "FFNetworking.h"
#import <AFNetworking.h>
#import "FFDefine.h"

static AFHTTPSessionManager *_mgr;

@implementation FFNetworking

+ (FFNetworking *)shareInstance
{
    static FFNetworking *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[FFNetworking alloc] init];
//        instance.taskStack = [NSMutableDictionary dictionary];
        _mgr = [AFHTTPSessionManager manager];
        _mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
        _mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"text/json", @"text/JavaScript",@"image/png", nil];
    });
    
    return instance;
}

- (void)httpRequestGet:(NSString *)requestUrl parmsDict:(NSDictionary *)parmsDict successed:(NetworkingSuccessedBlock)successedDict failure:(NetworkingFailureBlock)failureDict
{
    
    NSMutableDictionary *paramsM = [NSMutableDictionary dictionaryWithDictionary:parmsDict];
    
   NSLog(@"request_params \n%@",paramsM);


    [_mgr GET:requestUrl parameters:[paramsM copy] progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

        NSDictionary *resultDict = [FFCommonUtil utilJsonStringToDict:string];
        NSLog(@"%@",string);
        successedDict(resultDict);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failureDict(error);
    }];
}


+ (void)httpUplodWithRequestUrl:(NSString *)requestUrl parmsDict:(NSDictionary *)parmsDict fileData:(NSData *)fileData mimeType:(NSString *)mimeType fileName:(NSString *)fileName successed:(NetworkingSuccessedBlock)successedDict failure:(NetworkingFailureBlock)failureDict
{
//    KCUser *user = [KCUser loadUser];
//    NSMutableDictionary *paramsM = [NSMutableDictionary dictionaryWithDictionary:parmsDict];
//    [paramsM setObject:user.admin_name forKey:@"admin_name"];
//    [paramsM setObject:user.admin_password forKey:@"admin_password"];
    
    [_mgr POST:requestUrl parameters:parmsDict constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        // 上传文件
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat            = @"yyyyMMddHHmmss";
        NSString *str                         = [formatter stringFromDate:[NSDate date]];
        NSString *upFileName               = [NSString stringWithFormat:@"%@.jpg", str];
        NSLog(@"%@",upFileName);
        [formData appendPartWithFileData:fileData name:fileName fileName:upFileName mimeType:mimeType];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"uploadPic == %@",string);
        NSDictionary *resultDict = [FFCommonUtil utilJsonStringToDict:string];
        NSLog(@"%@",resultDict);
        successedDict(resultDict);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"上传失败\n%@",error);
    }];
}

@end
